<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<title>Delete Files</title>
		<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="edit-box" class="login-box">
<?php // Script 12.8 - delete.php 
/* This script delete Files from a treedb database. */

require ("../../protected/mysqlsettings.php");

// Connect and select:
$dbc = mysql_connect($server, $username, $password);
mysql_set_charset('utf8',$dbc);
mysql_select_db('treedb', $dbc);
	
if (isset($_GET['id']) && is_numeric($_GET['id']) ) { // Display the entry in a form: //

	// Define the query:
			$query="SELECT Name, ActivationDate, DateExpiry, Password, CodeGrade, CodeSubject, UserName from Files where Code={$_GET['id']}";
	if ($r = mysql_query($query, $dbc)) { // Run the query.
	
		$row = mysql_fetch_array($r); // Retrieve the information.
		$fileName = $row['Name'];

		// Make the form:
		print '<form action="delete.php" method="post">
		<p>Estàs segur que vols borrar aquest fitxer?</p>
		<p><h3>' . stripslashes($row['Code']) . '</h3>' .
		stripslashes($row['Name']) . '<br />
		<input type="hidden" name="id" value="' . $_GET['id'] . '" /> 
		<input type="submit" class="button" name="submit" value="Borrar!" />
		<input type="button" class="button" name="mainpage" value="Pàgina principal" onclick="location.href=\'index.php\'"/>
		</p>
		</form>'; 

	} else { // Couldn't get the information.
		print '<p style="color: red;">No es poden obtenir les dades a causa de:<br />' . mysql_error($dbc) . '.</p><p>La consulta executada és: ' . $query . '</p>';
	}

} elseif (isset($_POST['id']) && is_numeric($_POST['id'])) { // Handle the form. 
	
	// Define the query:
	$deleteQuery = "DELETE FROM Files WHERE Code={$_POST['id']} LIMIT 1";
	$r = mysql_query($deleteQuery, $dbc); // Execute the query.
	/*
	If we try to delete the physical file from uploads folder we have 2 problems:
		- If we do it with sftp protocol, the php function ssh2() doesn't works properly
		- If we do it directly (with the unlink() function), we don't have permissions to acces to the file and, if we try to add permissions to the public folder we can't.
	*/

	// Report on the result:
	if (mysql_affected_rows($dbc) == 1) { 
		print '<p>EL fitxer ha estat borrat!.</p>';
		echo '<input type="button" class="button" name="mainpage" value="Pàgina principal" onclick="location.href=\'index.php\'"/>';
//		unlink($deleteFile);
	} else {
		print '<p style="color: red;">No es pot borrar aquest fitxer a causa de:<br />' . mysql_error($dbc) . '.</p><p>La consulta executada és: ' . $query . '</p>';
	}

} else { // No ID received.
	print '<p style="color: red;">Ha ocorregut un error a la pàgina.</p>';
} // End of main IF.

mysql_close($dbc); // Close the connection.

?>
</div>
</body>
</html>